#include "view_prompt_message.h"
#include "display_ctrl.h"
#include "platform_assert.h"
#include "lfs_file_manager_auto.h"
#include "rtc.h"
#include "datetime.h"
#include "blecom.h"
#include "battery_ctrl.h"
#include "sport_his.h"
#include "display_ctrl.h"
#include "display_interface.h"
#include "datetime.h"
#include "heart_rate_interface.h"
#include "font/font.h"
#include "font_mngr.h"
#include "motor.h"
#include "app_trace.h"
#include "common/common.h"
#include "view_prompt_message.inc"
#include "utf8_to_unicode.h"
#include "arabic_reshaper.h"
#include <wchar.h>
#define LOG(fmt, ...) app_trace_log(fmt "\r\n", ##__VA_ARGS__)
void display_show_ancs_req(uint8_t flag)
{
    if (flag)
    {
        ancs_disp_ctrl.new_flag = 1;
        if (disp_ctrl.on == DISP_ON_DISABLE)
        {
            display_show(DISP_TYPE_ANCS, DISP_MODE_NORMAL);
        }
        else
        {
            if (display_get_present_type() == DISP_TYPE_ANCS)
            {
                display_show(DISP_TYPE_ANCS, DISP_MODE_NORMAL);
            }
            else
            {
                display_show(DISP_TYPE_ANCS, DISP_MODE_DOWN_MOVE);
            }
        }
    }
    else if (ancs_disp_ctrl.message_type != ATTR_ID_CALL)
    {
        display_show(DISP_TYPE_ANCS, DISP_MODE_NORMAL);
        int new_offset = ancs_disp_ctrl.disp_offset + ancs_disp_ctrl.next_offset; //偏移计算
        if (new_offset >= ancs_disp_ctrl.message_len)                             //回到显示首页
        {
            ancs_disp_ctrl.disp_offset = 0;
        }
        else
        {
            ancs_disp_ctrl.disp_offset = new_offset;
        }
    }
}

bool view_prompt_message_event(int32_t view, event_param_t param)
{
    switch (param.event)
    {
    case event_view_exit:
        motor_stop();
        ancs_message_package.ancs_msgType = ATTR_ID_RESERVE;
        return true;
#ifdef EVENT_PREVIOUS_VIEW
    case event_move_previous_view:
        motor_stop(); //关振动
        display_show_ancs_req(1);
        return true;
#endif
#ifdef EVENT_NEXT_VIEW
#ifdef EVENT_BUTTON_FRONT
    case event_button_front_press:
#endif
#ifdef EVENT_LEVEL_DOWN
    case event_move_level_down:
#endif
    case event_move_next_view:
    {
        int new_offset = ancs_disp_ctrl.disp_offset + ancs_disp_ctrl.next_offset; //偏移计算
        if (new_offset >= ancs_disp_ctrl.message_len)                             //回到显示首页
        {
        }
        else
        {
            ancs_disp_ctrl.disp_offset = new_offset;
            display_show(DISP_TYPE_ANCS, DISP_MODE_NORMAL);
        }
        motor_stop(); //关振动
        return true;
    }
#else
#ifdef EVENT_BUTTON_FRONT
    case event_button_front_press:
#endif
        motor_stop(); //关振动
        display_show_ancs_req(0);
        return true;
#endif

    case event_ble_disconnect:
        m_ancs_call_clear_handle();
        return true;
#ifdef EVENT_BUTTON_FRONT
    case BUTTON_EVENT_LONG_PRESS:
        if (param.source == event_source_button_front)
        {
            m_ancs_call_long_press_btn_handle();
            return true;
        }
#endif
#ifdef USE_TOUCHPAD
    case BUTTON_EVENT_TP_TAP_LONG:
        m_ancs_call_long_press_btn_handle();
        return true;
#endif
    }
    return false;
}
// #define P_MSG_ICON_X_POS 26
// #define P_MSG_ICON_Y_POS 2

#define TEXT_X1 0
#define TEXT_Y1 33
#define TEXT_X2 (79)
#define TEXT_Y2 (159)

#define CALL_X1 0
#define CALL_Y1 90
#define CALL_X_SIZE (CALL_X2 - CALL_X1 + 1)
#define CALL_Y_SIZE (CALL_Y2 - CALL_Y1 + 1)
#define CALL_X2 (79)
#define CALL_Y2 (159)


icon_info_t const sms_icon = {0x47f3, 0x47e9, zip_image_msg_sms};
icon_info_t const qq_icon = {0x3f3f, 0x557f, zip_image_msg_qq};
icon_info_t const wechat_icon = {0x5feb, 0x07e2, zip_image_msg_wechat};
icon_info_t const facebook_icon = {0x6dff, 0x23df, zip_image_msg_facebook};
icon_info_t const whatsapp_icon = {0xa7e8, 0x47f3, zip_image_msg_whatsapp};
icon_info_t const twitter_icon = {0x5e9f, 0x05df, zip_image_msg_twitter};
icon_info_t const skype_icon = {0x86ff, 0x05bf, zip_image_msg_skype};
icon_info_t const line_icon = {0xc7e8, 0x2fe0, zip_image_msg_line};
icon_info_t const linkedin_icon = {0, 0, zip_image_msg_linkedln};
icon_info_t const instagram_icon = {0, 0, zip_image_msg_instagram};
icon_info_t const tim_icon = {0, 0, zip_image_msg_tim};
icon_info_t const snapchat_icon = {0xef00, 0, zip_image_msg_snapchat};
icon_info_t const others_icon = {0xfd42, 0, zip_image_msg_other};
icon_info_t const *message_icons[16] = {
    &others_icon,  &sms_icon,   &wechat_icon, &qq_icon,       &qq_icon,        &facebook_icon, &others_icon,   &whatsapp_icon,
    &twitter_icon, &skype_icon, &line_icon,   &linkedin_icon, &instagram_icon, &tim_icon,      &snapchat_icon, &others_icon,
};

// const uint8_t message_img_tab[] = {FILE_IMG_D115_other_100_11,     FILE_IMG_D118_message_100_11,  FILE_IMG_D106_weixin_100_11,    FILE_IMG_D105_qq_100_11,
//                                    FILE_IMG_D105_qq_100_11,        FILE_IMG_D107_facebook_100_11, FILE_IMG_D112_messenger_100_11, FILE_IMG_D109_whatsapp_100_11,
//                                    FILE_IMG_D108_twitter_100_11,   FILE_IMG_D113_skype_100_11,    FILE_IMG_D110_line_100_11,      FILE_IMG_D121_in_100_11,
//                                    FILE_IMG_D111_instagram_100_11, FILE_IMG_D120_tim_100_11,      FILE_IMG_D119_snapchat_100_11,  FILE_IMG_D115_other_100_11};

void view_prompt_message_display(void)
{
    uint16_t *unicode_str = (uint16_t *)(&disp_ctrl.com_data.i_temp[4]);
    int x = 0;
    int y = 0;
    if (disp_ctrl.loop_cnt == 0)
    {
        disp_ctrl.auto_refresh_time = 1000;
        disp_ctrl.auto_close_delay = disp_ctrl.auto_refresh_time + 1000;
        if (ancs_disp_ctrl.new_flag) //更新数据需求
        {
            LOG("get_msg[%s]", ancs_message_package.ancs_msg.appAttrMessage);
            ancs_disp_ctrl.message_type = ancs_message_package.ancs_msgType;
            utf8_string_to_unicode_string((uint8_t *)ancs_message_package.ancs_msg.appAttrMessage, (uint8_t *)unicode_str,
                                          ((uint8_t *)(disp_ctrl.com_data.i_temp)) + sizeof(disp_ctrl.com_data.i_temp) - (uint8_t *)unicode_str);
            disp_ctrl.com_data.int16_temp[5] = arabic_reshaper(unicode_str);
            ancs_disp_ctrl.message_len = wcslen(unicode_str) * 2;
            ancs_disp_ctrl.new_flag = 0;
            ancs_disp_ctrl.disp_offset = 0;
        }
        disp_ctrl.com_data.int16_temp[0] = ancs_disp_ctrl.disp_offset;
        disp_ctrl.com_data.int16_temp[1] = 0;
        disp_ctrl.com_data.int16_temp[2] = 0;
        int x1 = 0;
        int y1 = 0;
        int x2 = 0;
        int y2 = 0;
        if (ancs_disp_ctrl.message_type == ATTR_ID_CALL)
        {
            x1 = CALL_X1;
            x2 = CALL_X2;
            y1 = CALL_Y1;
            y2 = CALL_Y2;
        }
        else
        {
            x1 = TEXT_X1;
            x2 = TEXT_X2;
            y1 = TEXT_Y1;
            y2 = TEXT_Y2;
        }
        int temp = draw_off_chip_string_calculate(x1, y1, x2, y2, unicode_str, 16);
        ancs_disp_ctrl.next_offset = temp & 0xffff;
        disp_ctrl.com_data.int16_temp[4] = temp >> 16;
        disp_ctrl.com_data.int16_temp[4] = (disp_ctrl.com_data.int16_temp[4] < CALL_X_SIZE) ? (CALL_X_SIZE - disp_ctrl.com_data.int16_temp[4]) / 2 + x1 : x1;
    }

    if (ancs_disp_ctrl.message_type == ATTR_ID_CALL) //来电通知显示区分处理
    {
        int x1 = disp_ctrl.com_data.int16_temp[4];
        int y1 = CALL_Y1 + disp_ctrl.com_data.int16_temp[1];
        int x2 = CALL_X2;
        int y2 = CALL_Y2;

        draw_on_chip_gray_image(22, 28, zip_image_msg_call_bg, 0x47e9);
        draw_on_chip_gif_array_filter_color(26, 32, zip_image_msg_call, sizeof(zip_image_msg_call) / sizeof(zip_image_msg_call[0]));

        uint32_t temp16 =
            draw_off_chip_string(x1, y1, x2, y2, (uint16_t *)((uint8_t *)unicode_str + disp_ctrl.com_data.int16_temp[0] + disp_ctrl.com_data.int16_temp[2]), 16, &disp_ctrl.com_data.int16_temp[5]);
        if (temp16)
        {
            disp_ctrl.com_data.int16_temp[2] += (temp16 & 0xffff);
            disp_ctrl.com_data.int16_temp[1] += ((temp16 >> 16) * (FONT16_SIZE + FONT_SIZE_VERTICAL_INTERVAL));
        }
        if (ancs_disp_ctrl.next_offset < ancs_disp_ctrl.message_len) //多页
        {
            draw_on_chip_image(x2 - 16, y2 - 16, IMAGE_USER_FONT_16X16_DOT);
        }
        if (disp_ctrl.loop_cnt == 0) //
        {
            if (disp_ctrl.auto_refresh_count >= 120)
            {
                m_ancs_call_clear_handle();
                display_show_type_recover();
                disp_update_set_loop_end();
            }
        }
        return;
    }
    if (disp_ctrl.loop_cnt == 0)
    {
        if (disp_ctrl.auto_refresh_count >= 10)
        {
            display_show_type_recover();
            return;
        }
    }
    // _disp_show_flash_img(DISP_LFS_FILE_INDEX_0, 100, 25, message_img_tab[ancs_disp_ctrl.message_type % sizeof(message_img_tab)]);
    icon_info_t const *icon_info = 0;
    if (ancs_disp_ctrl.message_type < sizeof(message_icons) / sizeof(message_icons[0]))
    {
        icon_info = message_icons[ancs_disp_ctrl.message_type];
    }
    else
    {
        icon_info = message_icons[ATTP_ID_OTHERS];
    }
    x = 26;
    y = 2;
    if (icon_info->start_color != 0)
    {
        if (icon_info->end_color != 0)
        {
            draw_on_chip_gradients_image(x, y, icon_info->image, icon_info->start_color, icon_info->end_color);
        }
        else
        {
            draw_on_chip_gray_image(x, y, icon_info->image, icon_info->start_color);
        }
    }
    else
    {
        draw_on_chip_image(x, y, icon_info->image);
    }
    uint32_t temp16 = draw_off_chip_string(TEXT_X1, TEXT_Y1 + disp_ctrl.com_data.int16_temp[1], TEXT_X2, TEXT_Y2,
                                           (uint16_t *)((uint8_t *)unicode_str + disp_ctrl.com_data.int16_temp[0] + disp_ctrl.com_data.int16_temp[2]), 16, &disp_ctrl.com_data.int16_temp[5]);
    if (temp16)
    {
        disp_ctrl.com_data.int16_temp[2] += (temp16 & 0xffff);
        disp_ctrl.com_data.int16_temp[1] += ((temp16 >> 16) * (FONT16_SIZE + FONT_SIZE_VERTICAL_INTERVAL));
    }
    if (disp_ctrl.com_data.int16_temp[0] != 0) //非首页的继续显示
    {
        if ((disp_ctrl.com_data.int16_temp[0] + ancs_disp_ctrl.next_offset) < ancs_disp_ctrl.message_len) //非最后一页
        {
            draw_on_chip_image(TEXT_X2 - 16, TEXT_Y2 - 16, IMAGE_USER_FONT_16X16_DOT);
        }
    }
    else
    {
        if (ancs_disp_ctrl.next_offset < ancs_disp_ctrl.message_len) //多页
        {
            draw_on_chip_image(TEXT_X2 - 16, TEXT_Y2 - 16, IMAGE_USER_FONT_16X16_DOT);
        }
    }
}
